//
//  Datasource.m
//
//  Created by 周成龙
//

#import "Datasource.h"

@interface Datasource ()

@property (nonatomic, copy) NSArray *items;
@property (nonatomic, copy) NSString *cellID;
@property (nonatomic, copy) void(^configBlock)(id model, id cell);

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation Datasource

- (instancetype)init {
    return nil;
}

- (instancetype)initWithTable:(UITableView *)tableView items:(NSArray *)items cellID:(NSString *)cellID block:(void(^)(id model, id cell))block {
    if (self = [super init]) {
        self.items = items;
        self.configBlock = block;
        self.cellID = cellID;
        self.tableView = tableView;
    }
    return self;
}

- (void)setDatas:(NSArray *)datas {
    _items = datas;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellID forIndexPath:indexPath];
    
    self.configBlock(self.items[indexPath.row], cell);
    
    return cell;
}

@end
